My R app produces extra additional output on the body - r

This is my R code :
library("RJSDMX")
library(shiny)
ui <- fluidPage(
titlePanel("Navlist panel example"),
navlistPanel(
"Header",
result <- array(getFlows("TurkStat")),
boyut1 <- getDimensions("TurkStat","RD_A"),
boyut2 <- getDimensions("TurkStat","STSALL"),
boyut3 <- getDimensions("TurkStat","NA_MAIN"),
selected = result[1],
# boy_tab1 <- as.data.frame.table(boyut1),
# boy_tab2 <- as.data.frame.table(boyut2),
# boy_tab3 <- as.data.frame.table(boyut3),
tabPanel(result[1], as.data.frame.list(boyut1)),
tabPanel(result[2], as.data.frame.list(boyut2)),
tabPanel(result[3], as.data.frame.list(boyut3))
)
)
server = function(input, output) { }
shinyApp(ui, server)
When the page loads the result output displays completely :
RD data structure definition STS ALL NA Main Aggregates tab-9265-2 SDMX/CL_FREQ/2.0 ESTAT/CL_GEO_RD/1.2 ESTAT/CL_SECTFUND/1.2 ESTAT/CL_SECTFUND/1.2 ESTAT/CL_SEX/2.0 ESTAT/CL_RD_OCCUP/1.1 ESTAT/CL_FORD/1.0 ESTAT/CL_AGE/2.0 ESTAT/CL_GEO_RD/1.2 ESTAT/CL_ISCED11_RD/1.0 ESTAT/CL_SENIORITY/1.0 ESTAT/CL_TYPECOST/1.1 ESTAT/CL_TYPERD/1.1 ESTAT/CL_AFFILIATION/1.0 ESTAT/CL_INDUSTRY/1.1 ESTAT/CL_INDUSTRY/1.1 ESTAT/CL_EMPLOYEES/1.1 ESTAT/CL_NABS07/1.1 ESTAT/CL_RD_UNIT/1.1 tab-9265-3 SDMX/CL_FREQ/2.0 IMF/CL_AREA/1.5 ESTAT/CL_ADJUSTMENT/1.4 ESTAT/CL_STS_INDICATOR/1.1 ESTAT/CL_ACTIVITY_STS/1.0 ESTAT/CL_BASE_YEAR/1.1 tab-9265-4 SDMX/CL_FREQ/2.0 ESTAT/CL_ADJUSTMENT/1.4 IMF/CL_AREA/1.7 IMF/CL_AREA/1.7 ESTAT/CL_SECTOR/1.5 ESTAT/CL_SECTOR/1.5 IMF/CL_ACCOUNT_ENTRY/1.2 ESTAT/CL_NA_STO/1.8 ESTAT/CL_INSTR_ASSET/1.6 ESTAT/CL_ACTIVITY/1.5 ESTAT/CL_COICOP/1.1 IMF/CL_UNIT/1.8 ESTAT/CL_NA_PRICES/1.1 ESTAT/CL_TRANSFORMATION/1.2 tab-9265-5
SDMX/CL_FREQ/2.0 ESTAT/CL_GEO_RD/1.2 ESTAT/CL_SECTFUND/1.2 ESTAT/CL_SECTFUND/1.2 ESTAT/CL_SEX/2.0 ESTAT/CL_RD_OCCUP/1.1 ESTAT/CL_FORD/1.0 ESTAT/CL_AGE/2.0 ESTAT/CL_GEO_RD/1.2 ESTAT/CL_ISCED11_RD/1.0 ESTAT/CL_SENIORITY/1.0 ESTAT/CL_TYPECOST/1.1 ESTAT/CL_TYPERD/1.1 ESTAT/CL_AFFILIATION/1.0 ESTAT/CL_INDUSTRY/1.1 ESTAT/CL_INDUSTRY/1.1 ESTAT/CL_EMPLOYEES/1.1 ESTAT/CL_NABS07/1.1 ESTAT/CL_RD_UNIT/1.1
I just want to display the result when I make a selection on the sidebar. But it shows my selection after displaying all the result.
Can you please help me how I can clear the body of the page to make it possible it behaves as according to my selections?
Thank you very much.
best regards.

Related

how to customize the variables shown in dropdown list

I am working on a SHINY app, and I am using a dropdown menu which comes with 100 different variables. I want to show only a subset of these. I could shorten the data.frame, but I need it to be this long for other reasons. Any ideas?
Thanks for your help.
this is what I have on server.R:
output$xvar <- renderUI(selectInput('xvar',label='I want to show only certain variables here', choices = names(df),selected = names(df)[1]))
Wrap the subset into a reactive and just render that:
library(shiny)
ui <- fluidPage(
uiOutput("xvar")
)
df <- 1:100
server <- function(input, output, session) {
dfsubset <- reactive({
df[1:10]
})
output$xvar <- renderUI(selectInput('xvar',label='I want to show only certain variables here',
choices = dfsubset(),selected = dfsubset()))
}
shinyApp(ui,server)

My gvisBubbleChart Plot in Shiny R stops displaying when I attempt to introduce a dynamic selectInput field

I have encountered this problem while developing an app, and reproduced it here in a simplified script using Fruits df.
Basically, i have selectInput box to select a Year, which is a column in Fruits. I create unique list of Years, and feed it into selectInput box.
Then, ideally, i wanted my plot to display only the records for the year I selected. However, as you'll see in my code - the second you uncomment a block of 3 lines to accomplish that, - the plot stops displaying even though there doesn't seem to be any errors. Anybody knows why is this? Thanks in advance!!!
Related question - while debugging this i saw that the input$explore_year is at first "Null". I'm trying to handle this in the code but not sure why the selected="2010" doesn't take care of it automatically.
library(shiny)
library(googleVis)
library(DT)
listOfFruits <- sort(unique(Fruits$Year), decreasing = FALSE)
ui <- fluidPage(title = "Fruits Bug Recreated",
fluidRow(
column(3,
wellPanel(
uiOutput("choose_year"),
br()
)),
column(9,
tags$hr(),
htmlOutput("view")
)),
fluidRow(DT::dataTableOutput("tableExplore"))
)
server <- function(input, output) {
output$view <- renderGvis({
#Uncomment these 3 lines to see how the plot stops displaying.
# local_exloreYear <- input$explore_year
# if (is.null(local_exloreYear)) {local_exloreYear <- "2010"}
# FruitsSubset <- subset(Fruits, Year == local_exloreYear)
#------------I wanted to use the commented line below instead of the
#that follows
#gvisBubbleChart(FruitsSubset, idvar="Fruit",
#-------------
gvisBubbleChart(Fruits, idvar="Fruit",
xvar="Sales", yvar="Expenses",
colorvar="Year", sizevar="Profit",
options=list(
hAxis='{minValue:70, maxValue:125, title:"Sales"}',sortBubblesBySize=TRUE,
vAxis='{title: "Expenses",minValue:60, maxValue:95}'
))
})
# Drop-down selection box for dynamic choice of minutes in the plans to compare
output$choose_year <- renderUI({
selectInput("explore_year", "Select Year", as.list(listOfFruits),selected ="2010")
})
output$tableExplore <- DT::renderDataTable(DT::datatable({
FruitsSubset <- subset(Fruits, Fruits$Year == input$explore_year)
myTable <-FruitsSubset[,c(1,2,3,4,5,6)]
data <- myTable
data
},options = list(searching = FALSE,paging = FALSE)
))
}
shinyApp(ui = ui, server = server)
Like i wrote in the comments you can solve it by make the rendering conditional on the input being non-NULL.
output$view <- renderGvis({
if(!is.null(input$explore_year)){
...
}
})
Nevertheless, I donĀ“t think it is really intended that you have to do that, as in other render functions it is not required e.g. in the DT::renderDataTable(), where you also use the same input (being NULL initially).
Therefore, I would suggest reporting it as a bug.

To create numericinput for all columns in a data set using renderui

I am trying to create numeric boxes for all column names in a data set. I have written below code but this displays a blank page. Not sure what the error is. Any suggestions?
library(shiny)
library(readr)
shinyApp(
ui <- fluidPage(
uiOutput("TestColumns")
),
server <- function(input, output) {
data_set <- read.csv("Data/170210_Flat_File.csv")
output$TestColumns <- renderUI({
for(i in names(data_set)){
numericInput(i, i,30)
}}
)})
First off, when you ask questions you should ALWAYS post a minimal reproducible example. That is basically something that we can run to replicate the issue you are having so that it is much easier for us to help you. This way we don't have to go about using different data, trying to figure out exactly what your error is. See this link for a good intro: How to make a great R reproducible example?
Next to your question - since you didn't explicitly post an error you were seeing or explicitly state what your issue was I'm going to go ahead and assume that your issue is that you don't see any UI's popping up when you run your Shiny App (this is what I got when I tried running your code with different sample data).
The reason you aren't seeing anything is because you aren't returning any objects from your for loop. If you really wanted to do a for loop you would have to loop through, store everything in a list, then return that list. Note that I had to use R's built in data because you didn't provide any. Something like this would work:
shinyApp(
ui <- fluidPage(
#numericInput("test","test",30),
uiOutput("TestColumns")
),
server <- function(input, output) {
data_set <- mtcars
output$TestColumns <- renderUI({
L<-vector("list",length(names(data_set)))
for(i in names(data_set)){
L[[i]]<-numericInput(i, i,30)
}
return(L)
})})
This should give you your desired result. However, the above is unnecessarily complicated. I suggest you use an lapply instead. Something like this is much better in my opinion:
shinyApp(
ui <- fluidPage(
#numericInput("test","test",30),
uiOutput("TestColumns")
),
server <- function(input, output) {
data_set <- mtcars
output$TestColumns <- renderUI({
lapply(names(data_set),function(x){numericInput(x,x,30)})
})})
ui <- bootstrapPage(
fluidRow(
column(4,offset = 2,
tags$h4("numeric inputs"),
uiOutput('mtcars_numerics') # These will be all the numeric inputs for mtcars
),
column(6,
tags$h4("current input values"),
verbatimTextOutput('show_vals') # This will show the current value and id of the inputs
)
)
)
server <- function(input, output, session){
# creates the output UI elements in the loop
output$mtcars_numerics <- renderUI({
tagList(lapply(colnames(mtcars), function(i){ # must use `tagList` `
column(3,
numericInput(
inputId = sprintf("mt_col_%s",i), # Set the id to the column name
label = toupper(i), # Label is upper case of the col name
min = min(mtcars[[i]]), # min value is the minimum of the column
max = max(mtcars[[i]]), # max is the max of the column
value = mtcars[[i]][[1]] # first value set to the first row of the column
))
})
)
})
# So we can see the values and ids in the ui for testing
output$show_vals <- renderPrint({
all_inputs <- names(session$input)
input_vals <- plyr::ldply(all_inputs, function(i){
data.frame(input_name = i, input_value = input[[i]],stringsAsFactors = FALSE)
})
input_vals
})
}
shinyApp(ui, server)
Results in:

How to embed a value from a reactive dataset in a tags() function in shiny?

In the shiny app below, I would like to print out parts of the two reactive datasets (reactiveDf and reactive2) using the function tags but the script I have is not working.
I know I could use other solutions like renderTable, but later on I need to embed this reactive code in a html page using {{}}, thus it would be wonderful if somebody can explain to me why this is not working.
library(shiny)
library(dplyr)
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
checkboxGroupInput('Category', '',
unique(mtcars$carb), selected = unique(mtcars$carb))),
# Show table of the rendered dataset
mainPanel(
tags$div(reactiveDf()),
tags$div(reactive2())
)
)
))
server <- shinyServer(function(input, output) {
reactiveDf <- reactive({return(tbl_df(mtcars) %>%
filter(carb %in% input$Category))})
reactive2 <- reactive({reactiveDf()[1,]})
})
shinyApp(ui = ui, server = server)

how dynamically update html table on shiny app

I would like to generate a new html table every time the user changes the parameter "mass" below, and then exhibit it dynamically
server.R
library(hwriter)
shinyServer(function(input, output) {
output$distPlot <- renderText({
mass <- as.numeric(input$mass)
win <- as.numeric(input$sswin)
m1 <- mass-win/2
m2 <- mass+win/2
etr <- paste0("http://rest.kegg.jp/find/compound/", m1, "-", m2, "/exact_mass")
tb <- read.table(etr)
colnames(tb) <- c("id", "mass")
#p <- openPage('test.html')
tblk <- cbind(paste0("http://www.kegg.jp/dbget-bin/www_bget?", tb[,1]), NA)
#hwrite(tb, p, link = tblk)
hwrite(tb, link = tblk)
#close(p)
})
})
ui.R
library(shiny)
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
textInput("mass", "Mass:", "200.05"),
textInput("sswin", "Search window:", "0.5")
),
# Show a plot of the generated distribution
mainPanel(
#plotOutput("distPlot"),
#includeHTML("test.html")
uiOutput("distPlot")
)
)
))
The table is being generated, but I don't know how to update it.
I figured out a way and edited above.
I encountered the very same problem as user1265067 and I found a solution to the problem.
Since this questions lacks an answer and in the case that other users stumble upon this thread, please find my solution (with a working toy example) in this thread: Shiny - populate static HTML table with filtered data based on input
In short, I wrap the static html table in a function, source it in the server and call it in the renderUI() function.

Resources