I am reading a csv file using shiny interface in a R markdown(RMD) file.
```{r, echo = FALSE}
shinyApp(
ui = fluidPage(
fluidRow(
column(3,
fileInput("file","Upload the file"),
helpText("Default max. file size is 5MB")
),
column(4,
tags$hr(),
h5(helpText("Select the read.table parameters below")),
checkboxInput(inputId = 'header', label = 'Header', value = TRUE),
checkboxInput(inputId = "stringAsFactors", "stringAsFactors", TRUE),
br()
),
column(5,
radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
),
mainPanel(
uiOutput("tb")
# use below code if you want the tabset programming in the main panel. If so, then tabset will appear when the app loads for the first time.
# tabsetPanel(tabPanel("Summary", verbatimTextOutput("sum")),
# tabPanel("Data", tableOutput("table")))
)
)
),
server = function(input, output) {
data <- reactive({
file1 <- input$file
if(is.null(file1)){return()}
read.table(file=file1$datapath, quote = NULL,header = TRUE, sep=input$sep, fill=TRUE,stringsAsFactors = input$stringAsFactors)
})
output$table <- renderTable({
if(is.null(data())){return ()}
head(data(),5)
})
output$tb <- renderUI({
if(is.null(data()))
return()
else
tabPanel("Data", tableOutput("table"))
})
},
)
```
Input data is now stored in data(). Later in my document i wish to create another shiny application and plot the histogram of this data.In this case i need to pass the variable data() to RMarkdown and later call that variable in the next shiny App. Is there any ways to do it?
Well, the solution to your problem is to create a layer application. Shiny does not work like html or php, you call the files and each file has its own code. Shiny only generates an html code (when you run the application).
Probably, you have some options to show the plot in a Shiny app:
http://shiny.rstudio.com/gallery/navbar-example.html
https://github.com/daattali/shinyjs
http://shiny.rstudio.com/reference/shiny/latest/conditionalPanel.html
In my experience, I used the navbar, to create a navigation panel, that you only see the selected menu. Moreover you can use the package shinyjs, that allows you to hide some elements, you when you want it.
Related
I am making a R shiny app that will import a csv file, run the table through a user defined function, and displays a plot and table. I'm not able to share the actual function, but have put an example function below for reference in the code.
fun <- function(dataset){
p_plot <- plot(dataset)
mm_table <- lm(y~x, data=dataset)
return (list(p_plot, mm_table))
}
dataset <- data.frame(x= 1:10, y=1:10)
fun(dataset)
So far, I have been able to make a shell for the app that has all the tabs and buttons I want.
library(shiny)
library(shinythemes)
library(dplyr)
ui <- shinyUI(fluidPage(
theme = shinytheme("readable"),
titlePanel("Plot Generator"),
tabsetPanel(
tabPanel("Upload File",
titlePanel("Upload Deisgn"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$br(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
actionButton("action", "Evaluate Design")
),
mainPanel(
tableOutput('contents')
)
)
),
tabPanel("Plot",
mainPanel(
plotOutput('p_plot'),
tableOutput('mm_table'),
)
)
)
)
)
server <- shinyServer(function(input, output, session) {
data <- reactive({
req(input$file1)
inFile <- input$file1
df <- read.csv(inFile$datapath, header = input$header, sep = input$sep,
quote = input$quote)%>% mutate(across(where(is.character), as.factor))
return(df)
})
output$contents <- renderTable({
data()
})
output$mm_table <- renderTable({
mm_table
})
output$p_plot <- renderPlot({
p_plot
})
})
shinyApp(ui, server)
I don't understand how to incorporate the user defined function I created. I want the evaluate design button to run the imported table through my function. Where do I put my function and then how can I call the return objects? I eventually want to create buttons that can change the default arguments, but I don't understand how to interact with the user defined function.
The output$mm_table and output$p_plot are place holders and I know it isn't correct. I don't know how to call return objects from a user defined function in general.
Any help with code, feedback, or online resources would be appreciated. Thanks.
I am new to R and R shiny, and have been working on putting together a statistics application that will allow the user to import files, and then run different statistics programs on the data. The fileData function had been working fine for me until recently, and now whenever I attempt to upload a file, nothing opens. I have tried everything I can think of to get it to run, but it appears the file won't attach to the function. Any help will be very much appreciated!
library(shiny)
library(shinyFiles)
library(dplyr)
library(shinythemes)
ui <- fluidPage(theme = shinytheme("cosmo"),
# Application title
titlePanel("Stats"),
# Sidebar
sidebarLayout(
sidebarPanel(
tabsetPanel(type = "tab",
tabPanel("SCI",
fileInput("file1", "Insert File", multiple = TRUE, accept = c("text/csv", "text/comma-separated-values, text/plain", ".csv")),
selectInput("statChoice", "Choose Stats", c("None" = "None", "ANOVA 0 w/in 1 btw" = "A1btw", "ANOVA 0 w/in 2 btw" = "A2btw")),
conditionalPanel("statChoice == 'A1btw'",
uiOutput("ind1"),
uiOutput("dep1")),
conditionalPanel("statChoice == 'A2btw'",
uiOutput("ind1"),
uiOutput("ind2"),
uiOutput("dep1")),
)
)
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(type = "tab",
tabPanel("Data",
dataTableOutput("fileData")),
tabPanel("Summary Statistics"),
tabPanel("Graphs"))
)
)
)
server <- function(input, output) {
fileData <- eventReactive(input$file1,{
read.csv(input$file1$dataPath, header = TRUE, sep = ",", dec = ".")
})
output$fileData <- renderDataTable(
fileData()
)
vars <- reactive({
names(fileData())
})
output$ind1 <- renderUI({
selectInput("var1", "Independent 1", choices = vars())
})
output$ind2 <- renderUI({
selectInput("var2", "Independent 2", choices = vars())
})
output$dep1 <- renderUI({
selectInput("var3", "Dependent 1", choices = vars())
})
}
shinyApp(ui = ui, server = server)
Tricky because Shiny doesn't give any warning about this :
shiny app will not work if the same "output" is used two times in Ui.R.
Everything looks OK, except the double use of uiOutput("dep1") and uiOutput("ind1") :
conditionalPanel("statChoice == 'A1btw'",
uiOutput("ind1"), # Used once
uiOutput("dep1")), # Used once
conditionalPanel("statChoice == 'A2btw'",
uiOutput("ind1"), # Used twice
uiOutput("ind2"),
uiOutput("dep1")), # Used twice
You should use an output only once.
I'm building a simple website using Shiny,that allow users to uplaod a csv,xls ... file within Getting the data tab and view it in another tab named Viewing the data and then plot that data in another tab visualizing the data . for instance i want just to render a table based on the data picked ,
Here's a snippet of what i tried :
ui :
ui <- fluidPage(
sidebarLayout(
sidebarPanel("APRIORI INPUTS",id="panelTitle"),
mainPanel(
tabsetPanel(
tabPanel(title = "Getting the data",icon = icon("database"),
tags$div(id="uploadFiles",
fileInput("file1", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"))
)
),
tabPanel(title = "Viewing the data",icon = icon("eye"),
tableOutput("Viewing_the_data")),
tabPanel(title = "visualizing the data",icon = icon("chart-bar"),
tableOutput("visualizing_the_data"))
)
),
)
)
for the server logic :
server :
server <- function(input, output){
output$Viewing_the_data <- renderTable({
req(input$uploadFiles)
read.csv(input$selection$datapath)
})
}
shinyApp(ui = ui, server = server)
I tried that but doesn't work ...
PS : i tried that with shinydashboard and it works perfectly as that : r shiny - display data frame after uploading
Any suggestions or advice would be appreciated. Thanks.
I've tried to adapt the example I mentioned with your app, removing some of the complexity to make it a simpler app but still have the tabbed structure and it works when I run it. I can choose the file in one tab, select how many rows to show in the sidebar and show the data in another tab:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel("APRIORI INPUTS",id="panelTitle",
numericInput("n", "Rows", value = 5, min = 1, step = 1)),
mainPanel(
tabsetPanel(
tabPanel(title = "Getting the data", fileInput("file", NULL, accept = c(".csv", ".tsv")), icon = icon("database")),
tabPanel(title = "Viewing the data", tableOutput("head"), icon = icon("eye")))
)
)
)
server <- function(input, output, session) {
data <- reactive({
req(input$file)
ext <- tools::file_ext(input$file$name)
switch(ext,
csv = vroom::vroom(input$file$datapath, delim = ","),
tsv = vroom::vroom(input$file$datapath, delim = "\t"),
validate("Invalid file; Please upload a .csv or .tsv file")
)
})
output$head <- renderTable({
head(data(), input$n)
})
}
# Run the application
shinyApp(ui = ui, server = server)
I am pretty new to shiny.
I am working on an application and at one point it gives the option of either uploading a csv file or using text input to generate the 'csv'(it's actually a data.table internally). Depending on the selection I would like either the sidebarpanel to extend and load the upload widget (or the text input to appear in the main panel)
At the moment the upload-widget shows up right when the app is loaded.
I appreciate any help!
ui <- shinyUI(fluidPage(
tagList(
navbarPage( id = 'mynavlist', "My App",
tabPanel("Create Boolean Gates",
sidebarPanel(
radioButtons("radio", label = p("Choose one option"),
choices = list("Upload Template" = 1, "Create Template" = 2),
selected = 1),
tags$hr(),
####only when selection is 'Upload Template'
uiOutput("templ_upload"),
tags$hr()
),
mainPanel(
#####only when upload was selected and after uploading the csv file
tableOutput(outputId = 'table'),
####only when selection is 'Create Template'
uiOutput("templ_create")
)
)
))))
server <- shinyServer(function(input, output) {
#### display upload widget if 'upload template' is chosen
output$templ_upload <- renderUI({
fileInput(inputId = 'templ_file', label = 'Choose a Template in csv
format')
tags$hr()
checkboxInput('header', 'Header', TRUE)
radioButtons('sep', 'Separator',
c(Comma=',',Semicolon=';',Tab='\t'))
})
####show the data after upload in mainpanel
output$table <- renderTable({
if (is.null(input$table)){
h5("You have not uploaded a valid file")
}else{
template_csv <- fread(input$table$datapath, header=input$header,
sep=input$sep,quote=input$quote, check.names = FALSE)
return(template_csv)
}
})
####to be finished
# output$templ_create <- renderUI({
# })
})
shinyApp(ui = ui, server = server)
A conditionalPanel() could be used, but I think in this case it is easier to
specify these conditions in the renderUI():
(DonĀ“t forget to use a tagList() if you want to pass multiple UI elements from the renderUI())
output$templ_upload <- renderUI({
if(input$radio == 1){
tagList(
fileInput(inputId = 'templ_file', label = 'Choose a Template in csv
format'),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',Semicolon=';',Tab='\t'))
)
}
})
output$templ_create <- renderUI({
if(input$radio == 2){
textInput("table", "Table", "Sample text")
}
})
Not sure if this has been asked before. I am very new to working with RShiny apps, and I would like to use the values from a particular column of a particular CSV file for the choices in my selectInput() select box. Here is my code without the CSV, using some dummy variables.
ui <- shinyUI(fluidPage(
titlePanel(title = h4("PLAYER SELF-CENTERED RATING (PSCR)", align = "center")),
sidebarLayout(
sidebarPanel(
selectInput("selectplayer",
label = h3("Select box"),
choices = list("Choice 1" = 3,
"Choice 2" = 4,
"Choice 3" = 5),
selected = 3)
),
mainPanel(
plotOutput('radarPlot', width = "100%")
)
)
))
quite frankly, I'm fairly lost w.r.t where to begin on this. I also will need to use data from the CSV file to create another dataframe that is plotted in a renderPlot() call in shinyServer, so will need to find a way to get the CSV data into both server and ui. Is this a simple task, or something difficult? any help appreciated!
You can display uiOutput in ui and dynamically generate the ui in server. The code below should give you a hint.
library(shiny)
server <- function(input, session, output) {
# read csv here
datin <- read.table(text = 'Name,Age,Weight
John,10,40
Hary,20,70
Mike,30,80',
header = TRUE, sep =",", stringsAsFactors = FALSE)
output$select_1 = renderUI({
selectInput("select_input","select", choices = datin['Name'])
})
}
ui <- fluidPage(
uiOutput("select_1")
)
shinyApp(ui = ui, server = server)
You can generate dynamic output using uiOutput in the sidebar as shown in the following code:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "R-shiny app"),
dashboardSidebar(
uiOutput("columnNames") # Dynamic generate UI element
),
dashboardBody(
fluidRow(
column(10,
dataTableOutput('dataview')) #Display data in the tabular form
),
fluidRow(column(3, verbatimTextOutput("column_value"))),
hr()
)
)
server <- function(input, output) {
# Read data from .csv file
data=iris # (for understanding I am using iris dataset)
output$column_value <- renderPrint({
output$columnNames <- renderUI({
selectInput("datacolumn", h4("Select Data Column"), colnames(data)) # Dynamically set selectInput
})
output$dataview <- renderDataTable(data,options = list(pageLength = 10)) # Display the iris dataset
})
}
shinyApp(ui, server)