Wanted to check if I can convert server.R file into HTML. Sample server file below
server.R
library(shiny)
shinyServer(function(input, output) {
output$greeting <- renderText({
paste0("Hello, ", input$name, "!")
})
print(getwd())
})
You can, try shinyAce. See my example below. It not only gives you the nice html box around it and also r syntax highlight.
library(shiny)
library(shinyAce)
# load your server file for the right path
server_text <- readLines("server.R")
ui <- fluidPage(
aceEditor(outputId = "show_server", value = server_text, readOnly = TRUE, mode = "r")
)
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
Related
I'd like to make a shiny app that pulls a value from the url, but doesn't need to have an input element to work. E.g. I know I could do:
library(shiny)
shinyApp(
ui = fluidPage(
textInput("text", "Text", ""),
textOutput("outtext")
),
server = function(input, output, session) {
output$outtext <- renderText(input$text)
observe({
query <- parseQueryString(session$clientData$url_search)
if (!is.null(query[['text']])) {
updateTextInput(session, "text", value = query[['text']])
}
})
}
)
and that would pull from the app's url after /?text=abc, but what I'd really like is to be able to print the value from the url without having a textInput box. Is this possible?
Yes; render the query parameter directly:
library(shiny)
shinyApp(
ui = fluidPage(
textOutput("outtext")
),
server = function(input, output, session) {
output$outtext <- renderText(getQueryString()[["text"]])
}
)
I want to insert a fixed hyperlink in a shiny::info pop up
library(shiny)
library(shinyjs)
library(shinydashboard)
ui <- (
fluidPage(
useShinyjs(),
div(
id = "main_page",
fluidRow( # -------------------------------------------------------
infoBox(title=NULL, icon=shiny::icon(""), subtitle = HTML("<a id=\"infobutton\"
href=\"#\" class=\"action-button\"><i class=\"fa fa-info-circle\"></i></a>"))
)
)
)
)
server <- (
function(input, output, session) {
observeEvent(input$infobutton, {
shinyjs::info("It's me Mario")
})
}
)
shinyApp(ui = ui, server = server)
I've tried with a TagList but the pop up just display what's inside the tagList
shinyjs::info(tagList("It' me Mario:", a("Mario", href="https://en.wikipedia.org/wiki/Mario")))
Thanks !
You cannot (or should not) be able to insert HTML into there. It only supports plain text.
shinyjs::info() is running the javascript alert() function - here's the official documentation for it.
Notice the message parameter is:
A string you want to display in the alert dialog, or, alternatively, an object that is converted into a string and displayed.
It's not meant to accept HTML. I'm honestly very surprised that it's able to parse HTML within RStudio, browsers are supposed to only show plain text. If you want to show a pop up message with HTML you need to use something more advanced like shinyalert package or shiny modals.
You can directly generate the needed HTML with HTML:
library(shiny)
library(shinyjs)
library(shinydashboard)
ui <- (
fluidPage(
useShinyjs(),
div(
id = "main_page",
fluidRow( # -------------------------------------------------------
infoBox(title=NULL, icon=shiny::icon(""), subtitle = HTML("<a id=\"infobutton\"
href=\"#\" class=\"action-button\"><i class=\"fa fa-info-circle\"></i></a>"))
)
)
)
)
server <- (
function(input, output, session) {
observeEvent(input$infobutton, {
shinyjs::info(HTML("<p>It's me Mario:</p> <a href='https://en.wikipedia.org/wiki/Mario'>Mario</a>"))
})
}
)
shinyApp(ui = ui, server = server)
I'm new to RShiny and I decided to create my first App.
When I lauch my app with this command it executes right away and everything is fine.
shinyApp(ui, server)
But when I'm trying to deploy my app to the Shinyapps.io servers I have this error
Error: Unhandled Exception: Child Task 547685425 failed: Error parsing
manifest: Bundle does not contain manifest file: data/données.xlsx
It looks like Shiny can't find my document données.xlsx which is where I'm reading data for my apps but the first thing I do in my code is setting my directory.
Here is my Code :
setwd("C:/Users/Baillargeon/Desktop/R_PROG/RShiny_test")
library(shiny)
library(shinydashboard)
library(DT)
library(rsconnect)
library(ggplot2)
library(plotly)
library(dplyr)
library(xlsx)
donnees <- read.xlsx("data/données.xlsx", sheetName = "donnees", encoding = "UTF-8")
[...]
ui <- dashboardPage(
dashboardHeader(title = "Employés"),
dashboardSidebar(
sidebarMenu(
menuItem("Jeu de données",tabName="Donnees",icon=icon("database")),
menuItem("Graphiques",tabName="graph",icon=icon('signal'))
)
),
dashboardBody(
tabItems(
tabItem(tabName="Donnees",
h2("Données"),
DT::dataTableOutput("donnees")
),
tabItem(tabName = "graph", h2("Graphiques"),
fluidRow(
box(plotlyOutput("plot_sites")),
box(plotlyOutput("plot_sexe"))
)
)
)
)
)
server <- function(input,output){
output$donnees = DT::renderDataTable({
donnees
})
output$plot_sites <- renderPlotly({
plot_ly(final_sites, labels= final_sites$Site, values= final_sites$Freq, type="pie",
textposition = 'inside',
textinfo = 'label+percent',
showlegend = FALSE
) %>%
layout(title="Répartition des employés selon l'arondissement")
})
output$plot_sexe <- renderPlotly({
plot_ly(final_sexe, labels= final_sexe$Sexe, values= final_sexe$Freq, type="pie",
textposition = 'inside',
textinfo = 'label+percent',
showlegend = FALSE
) %>%
layout(title="Répartition des employés selon leurs Sexe")
})
}
shinyApp(ui, server)
rsconnect::deployApp("C:/Users/Baillargeon/Desktop/R_PROG/RShiny_test")
Does anyone know how to solve this error ?
Thanks
remove setwd("C:/Users/Baillargeon/Desktop/R_PROG/RShiny_test") as first line.
When you are deploying on shinyapps.io , it is a linux environment. so C:/ makes no sense.
Moreover, by default your working directory is where your ui.R and server.R are located. So, your data folder path should be relevant to that. Which looks like so from your code.
read documentation 9.4 “Disconnected from server” messages
Hope it helps
I am building an app that presents a data table and which allows you to add data. The adding of data is build by means of a form. This form is written by a module. What I want to happen is that one may fill out the form, press an 'add' button and that the data inside the table is updated.
As an example, could you help me figure out how to complete the following piece of code:
library(shiny)
library(shinydashboard)
moduleInput <- function(id){
ns <- NS(id)
sidebarPanel(
actionButton(ns("action1"), label = "click")
)
}
module <- function(input, output, session){
observeEvent(input$action1, {
# Do stuff here,
# -> let the parent module or server know that something has happened
})
}
ui <- fluidPage(
verbatimTextOutput("module.pressed"),
moduleInput("first")
)
server <- function(input, output, session){
# print the currently open tab
output$module.pressed <- renderPrint({
#-> Write that we have pressed the button of the module
})
callModule(module,"first")
}
shinyApp(ui = ui, server = server)
All I thus want to do is find an easy way to present TRUE in the output field module.pressed when something happend inside the module.
Thanks!
Modules can pass reactive expression(s) to calling apps/modules by returning them in their server function. The documentation provides a few examples on how to set up interactions between modules and calling apps - https://shiny.rstudio.com/articles/modules.html
If a module needs to use a reactive expression, take the reactive expression as a function parameter. If a module wants to return reactive expressions to the calling app, then return a list of reactive expressions from the function.
library(shiny)
moduleInput <- function(id){
ns <- NS(id)
sidebarPanel(
actionButton(ns("action1"), label = "click")
)
}
module <- function(input, output, session){
action1 <- reactive(input$action1)
return(reactive(input$action1))
}
ui <- fluidPage(
verbatimTextOutput("module.pressed"),
moduleInput("first")
)
server <- function(input, output, session){
action1 <- callModule(module,"first")
output$module.pressed <- renderPrint({
print(action1())
})
}
shinyApp(ui = ui, server = server)
I'am using the R package plotly in my shiny application.
Here is a small example:
library(shiny)
library(plotly)
ui <- fluidPage(
plotlyOutput("plot"),
verbatimTextOutput("event")
)
server <- function(input, output) {
# renderPlotly() also understands ggplot2 objects!
output$plot <- renderPlotly({
plot_ly(mtcars, x = ~mpg, y = ~wt)
})
output$event <- renderPrint({
d <- event_data("plotly_hover")
if (is.null(d)) "Hover on a point!" else d
})
}
shinyApp(ui, server)
When I click on the icon to download the graphic, the name of the graphic is "newplot":
How can I change the name "newplot(27)" by for example "ResultDNA(27)"
Thanks.
Currently there are two solutions:
By using export function in plotly
App1 <- plot_ly()
export(App1 , file = "ResultDNA(27).png")
You can 1st save the image in HTML and then in desired format using htmlwidgets
App2 <- plot_ly(...)
htmlwidgets::saveWidget(App2, file = "ResultDNA(27).html")