How to upload RData from ShinyFiles - r

I want to upload RData files using ShinyFiles, but I don´t know how to do it.
This is not working for me:
library(shiny)
library(shinydashboard)
library(shinyFiles)
# Define UI for application that draws a histogram
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
shinyFilesButton('files', label='File select', title='Please select a file', multiple=FALSE),
verbatimTextOutput("txt")
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
shinyFileChoose(input, 'files', root=c(root='/'), filetypes=c('', 'RData'))
output$txt <- renderPrint(
ls(parseFilePaths(roots= "/",selection = input$files))
)
}
# Run the application
shinyApp(ui = ui, server = server)
What´s wrong? I really dont understand how parseFilesPaths really works, because it get the route to the file, but I can not make it working. Also I have tried with
files< - load(parseFilePaths(roots= "/",selection = input$files)$type)
But it also didnt work...
Thansk!!

I have solved it. Here the solution (hope this will be helpfull for others)
# Define UI for application that draws a histogram
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
shinyFilesButton('files', 'File select', 'Please select a file', FALSE),
verbatimTextOutput('filepaths')
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
roots = c(wd='/home/developer/')
reactivity <- reactive({
shinyFileChoose(input, 'files', roots=roots, filetypes=c('', 'RData'))
ruta <- parseFilePaths(roots, input$files)$datapath
load(ruta)
return(parseFilePaths(roots, input$files))
})
output$filepaths <- renderPrint({
reactivity()
})
}
# Run the application
shinyApp(ui = ui, server = server)

Related

Shiny seesion object is not found when trying to use shinyJS()

In the shiny app below Im trying to use shinyJS() to hide and display text but I get:
Error: shinyjs: could not find the Shiny session object. This usually happens when a shinyjs function is called from a context that wasn't set up by a Shiny session.
Do not bother that dataset does not exist its just an example
## app.R ##
library(shiny)
library(shinydashboard)
library(dplyr)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(title = "Biodiversity"),
dashboardSidebar(
actionButton("action","Submit")
),
dashboardBody(
useShinyjs(),
show(
div(id='text_div',
verbatimTextOutput("text")
)
),
uiOutput("help_text"),
plotlyOutput("plot")
)
)
server <- function(input, output) {
output$help_text <- renderUI({
HTML("<b>Click 'Show plot' to show the plot.</b>")
})
react<-eventReactive(input$action,{
hide("help_text")
omited <-subset(omited, omited$scientificName %in% isolate(input$sci)&omited$verbatimScientificName %in% isolate(input$ver))
})
}
shinyApp(ui = ui, server = server)
You can't use show() in the ui, these functions are used in the server. Remove that and it works. Sample:
## app.R ##
library(shiny)
library(shinydashboard)
library(dplyr)
library(shinyjs)
library(plotly)
ui <- dashboardPage(
dashboardHeader(title = "Biodiversity"),
dashboardSidebar(
actionButton("action","Submit")
),
dashboardBody(
useShinyjs(),
div(id='text_div',
verbatimTextOutput("text")
)
,
uiOutput("help_text"),
plotOutput("plot")
)
)
server <- function(input, output) {
output$help_text <- renderUI({
HTML("<b>Click 'Show plot' to show the plot.</b>")
})
observeEvent(input$action,{
hide("help_text")
output$plot <- renderPlot({
plot(1)
})
})}
shinyApp(ui = ui, server = server)
Output:

Paste function not working in title of shiny app in R

I am trying to add some text to the current title in shiny app. So
I used
paste("Hello Shiny!", textOutput("text1"))
in the title but unfortunately something is wrong. The output and R code are as follows:
library(shiny)
ui <- fluidPage(
titlePanel(
title=
#textOutput("text1")#
paste("Hello Shiny!", textOutput("text1")))
)
server <- function(input, output) {
output$text1<-renderText({"Title changed dynamically"})
}
shinyApp(ui = ui, server = server)
What's wrong with the code?
Paste the data on the server side -
library(shiny)
ui <- fluidPage(
titlePanel(
title= textOutput("text1"))
)
server <- function(input, output) {
output$text1<-renderText({
dynamic_title <- "Title changed dynamically"
paste("Hello Shiny!", dynamic_title)})
}
shinyApp(ui = ui, server = server)

Disable and enable actionButton() by pushing another actionButton() in a shiny app

I have the shiny app below with 2 actionButton(). I want when I press Datatable the Datatable2 to be disabled and when I click again on Datatable the Datatable2 to be available for pressing again.
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
actionButton("exc","Datatable"),
actionButton("exc2","Datatable2")
),
mainPanel(
)
)
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
This is really straightforward if you use the toggleState() function from the shinyjs package.
The help for that function gives you an extremely similar situation. In your case:
library(shiny)
ui <- fluidPage(
useShinyjs(), #this activates shinyjs
sidebarLayout(
sidebarPanel(
actionButton("exc","Datatable"),
actionButton("exc2","Datatable2")
),
mainPanel(
)
)
)
server <- function(input, output) {
observeEvent(input$exc, {
toggleState("exc2") #identify the element to toggle between active/inactive
})
}
shinyApp(ui = ui, server = server)

renderUI in R shiny doesn't display

Sometimes we'd like to put content in a uiOutput/renderUI. But this doesn't always work. For instance, the example below. In my mind, code#1 and code#2 should give me the same GUI. However, code#2 doesn't work as expected. Can anyone tell me the reason? Thanks!
Code#1:
library(shiny)
ui <- navbarPage("test",
navbarMenu("More",
tabPanel("Table"
)
)
)
server <- shinyServer(function(input, output, session) {
})
shinyApp(ui = ui, server = server)
Code#2:
library(shiny)
ui <- navbarPage("test",
uiOutput("ui_data")
)
server <- shinyServer(function(input, output, session) {
output$ui_data <- renderUI({
navbarMenu("More",
tabPanel("Table"
)
)
})
})
shinyApp(ui = ui, server = server)
In the second example, uiOutput wraps the content of navbarMenu inside a div with the class "shiny-html-output". Divs of this class are however not allowed as an argument for navbarPage. AFAIK, there are two ways to resolve this
The first is to create the whole navbarPage on the server-side.
library(shiny)
ui <- uiOutput("page")
server <- shinyServer(function(input, output, session) {
output$page <- renderUI({
navbarPage("test", navbarMenu("More", tabPanel("Table")))
})
})
shinyApp(ui, server)
The other one is to only create the contents of the tabPanel in the server
library(shiny)
ui <- navbarPage(
"test",
navbarMenu("More", tabPanel("Table", uiOutput("tab_content")))
)
server <- shinyServer(function(input, output, session) {
output$tab_content <- renderUI({
"Some content"
})
})
shinyApp(ui = ui, server = server)
Please try to set your working directory first like example below.
setwd("c:/Users/ID/Desktop/folder")
You should get working directory with location of ui.R and server.R.

How can you pass a url to an iframe via textInput() in r shiny?

I need to embed a webpage reached through a URL inputted by the user.
I found this script but I can't make the iframe depend on a textInput() containing a URL. This example fails and I am not sure why.
library(shiny)
ui <- fluidPage(
textInput('url','url',value = "www.google.com"),
uiOutput('o')
)
server <- function(input, output, session) {
output$o = renderUI({
tags$iframe(src=input$url)
})
}
shinyApp(ui, server)
You can do like this:
library(shiny)
ui <- fluidPage(titlePanel("Getting Iframe"),
sidebarLayout(
sidebarPanel(
textInput("url", label = "Enter url"),
actionButton("go", "Go")
),
mainPanel(
htmlOutput("frame")
)
))
server <- function(input, output) {
output$frame <- renderUI({
validate(need(input$go, message=FALSE))
tags$iframe(src=isolate(input$url), height=600, width=535)
})
}
shinyApp(ui, server)

Resources