How to use OnClick action buttons in R ShinyDashBoard - shiny

I am doing sentiment analysis of twitter data using R and shiny dashboard.
I have plotted the persentages valueboxes of positive, negative and neutral tweets.
valueBoxOutput("positivePercent",width = 3)
output$positivePercent <- renderValueBox({
positivePercent <- round((nrow(positive)*100)/total)
valueBox(
value = paste(positivePercent, "%"),
subtitle = "Positive",
color = "green",
icon = icon("thumbs-up")
)
})
And I have shown what are the positive tweets using
tabItem(tabName = "pTweets",
h2(HTML("<center><b><span style='color: green'>Positive</span> Tweets <i></i></b></center>")),
tableOutput("positivitweets")
Now here I would like when I click that percentage value box then it should show positive tweets below of that box.
I am completely new to HTML and JAVASCRIPT,started to learn.

Related

rPivotTable in Shiny showing black areas outside of pivot when scrolled

I an using rpivotTable in a Shiny app
in UI it is inside a tabSetPanel:
tabPanel("Pivot Table",
shinycssloaders::withSpinner(
rpivotTableOutput("pivot_table")
)
)
in Server it is updated once the dataframe is loaded and filtered:
output$pivot_table <- renderRpivotTable({
rpivotTable(data = filtered_df(), rows=c("HIGH_LEVEL_GROUP_TERM", "HIGH_LEVEL_TERM", "AE_AS_PREFERRED_TERM"),
vals = "SAPPHIRE_CASE_ID_VERSION", aggregatorName = "Count Unique Values", rendererName = "Heatmap", width="100%")
})
When I scroll down the page, anything not observable immediately seems to have black on the outside of the pivot table?
I am using "shinydashboard"....

R Flexdashboard, how to get map to show in dashboard?

library(flexdashboard)
library(tidycensus)
library(tidyverse)
library(mapview)
library(dplyr)
library(tigris)
library(leafsync)
library(leafpop)
library(readxl)
library(DT)
library(highcharter)
{r}
#Importing Data ****Update file path before running****
states=data.frame(read_excel('C:/Users/pmckercher/Documents/CISA Research/CISA Funding Data.xlsx',
sheet = 'Funding By State',col_names = TRUE ))
us_geo=tigris::states(cb=FALSE, resolution = '20m') #State Sahpe File Download
all_data=inner_join(us_geo,states, by=c("NAME"="State")) #Joinging the two datasets
# These are shells, replace zcol= '' with a percentage of use column (will have to
#update the python file to calculate this when we get the real data) to make the display
#show how much of each states allocation has been provided.
Map22=mapview(all_data,zcol=c('fund_22'),col.regions = RColorBrewer::brewer.pal(11, "RdYlGn"), alpha.regions = 1, popup= popupTable(all_data,zcol=c('NAME', 'fund_22','fund_23','fund_24','fund_25')), label=c('NAME'))
Map22
This produces this visualization in the viewer:
Mapview Interactive Map
But, when the Dashaboard window pops up. it produces this where the map should be:
Buffering bars
How do I get the Interactive map to show up in the Dashboard window?

How do I size a Shiny app within an R-Markdown document using the Tufte Template?

I am writing a report using R-Markdown and the Tufte Template. I have embedded Shiny apps within the report.
So far I have tried using "fig.width=" and "width=" in the chunck options to no avail, then tried ",
options = list(height = 600) " just before the closing parenthesis as suggested by B. Davis change dimensions of shiny app embedded in r markdown HTML and again no change.
hist_dist <- read.csv("hist_dist2.csv", check.names = FALSE)
inputPanel(
selectInput("dist", label = "Choose an ecosystem to explore historical disturbance regimes:", choices = hist_dist$`Ecosystem`, selected = "Northern Hardwoods Forest" , width = '70%')
)
renderPlotly({
ggplot(filter(hist_dist, Ecosystem == input$dist), aes(x=Disturbance, y=Acres)) +
geom_bar(stat="identity", fill = "darkslategrey") +
coord_flip() +
theme(legend.position='none') +
expand_limits(y=c(0, 60)) +
theme_bw() +
theme(panel.background = element_rect(colour = "grey", fill=NA, size=1),
text = element_text(family = "serif", size = 14),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")
)
}
)
'''
I am able to change the width if I use other templates (non-Tufte), and are able to control the size of figures, just not Shiny apps.
I have been through the same situation. Tufte_html has something overwriting width behind the scene.
Here's my solution. Not the best but works. Basically you need to assign id to the plot and then change style from CSS.
Edit
output$my_plotly <- renderPlotly(❴❵)
Add
plotlyOutput("my_plotly") in the same chunk.
Write styles.css with
#my_plotly ❴ width: 55% !important❵
Tufte style uses 55% I believe.

Shiny, create score from selectInputs

I'm very new to shiny and having some trouble creating a simple app. I am trying to use selectInputs to create a score. For example: if question 1=true and question 2=true then output value=2; if question 1=false and question 2=true then output value =1; and so on. I think the problem may be that I'm not grasping how reactivity works despite several reading the documentation (a lot). I also tried it with radio buttons and actions buttons with no luck.
Here's what I've got on the ui side:
ui.R
shinyUI(fluidPage(
titlePanel("title"),
sidebarLayout(
sidebarPanel(
selectInput("var1",
label = "Some question 1",
choices = c("True", "False"),
selected = "True"),
selectInput("var2",
label = "Some question 2",
choices = c("True", "False"),
selected = "True"),
submitButton(text = "Submit", icon = NULL, width = NULL)
)
)
)
)
The use of submitButton is generally discouraged in favor of the more versatile actionButton (which increases by 1 each time it is pressed).
In your server.R code you can create a reactive (calc_score) which takes a dependency on the input$submit (i.e. the actionButton) and then calculates the score (here checkboxes are used since TRUE and FALSE equals 1 and 0, respectively, so the sum of your checkbox values gives the score). Using isolate avoids a dependency on input$var1 and input$var2, so calc_score does not trigger when the checkboxes change (only when input$submit changes, i.e. the actionButton is pressed).
Finally, calc_score only triggers if it is actually consumed by an output (in this case in the output$score function which renders text that is then included in the UI via verbatimTextOutput("score"))
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("title"),
sidebarLayout(
sidebarPanel(
checkboxInput("var1", label = "Some question 1", value = TRUE),
checkboxInput("var2", label = "Some question 2", value = TRUE),
actionButton(inputId = "submit", label = "Submit", icon = NULL, width = NULL)
),
mainPanel(
verbatimTextOutput("score")
))))
server <- function(input, output) {
calc_score <- reactive({
input$submit
isolate(sum(c(input$var1, input$var2)))
})
output$score <- renderText({
calc_score()
})
}
shinyApp(ui = ui, server = server)

Can typeahead be implemented over a dynamically changing dataframe using shinysky?

I am trying to populate a Typeahead box in Shiny, using the ShinySky package in R.
I'm trying to extend the example, where the data used to prepopulate the Typeahead is hardcoded into the textInput.typeahead function:
textInput.typeahead(
id="thti"
,placeholder="type 'name' or '2'"
,local=data.frame(name=c("name1","name2"),info=c("info1","info2")) #<- LOOK!
,valueKey = "name"
,tokens=c(1,2)
,template = HTML("<p class='repo-language'>{{info}}</p> <p class='repo-name'>{{name}}</p> <p class='repo-description'>You need to learn more CSS to customize this further</p>")
)
Having a local dataframe defined in the middle of the function is not what I would like to do, as the example has done here:
,local=data.frame(name=c("name1","name2"),info=c("info1","info2"))
I would like to supply an argument to local that is a reactive object, which is created elsewhere in Shiny.
So far I've been unable to do so.
Here's my strategy for attempting to populate the Lookhead options dynamically using reactivity:
1) Let the user subset a dataframe using a slider.
2) Set up the Lookahead to read in the subsetted dataframe, using something like ,local=subset(DF)
3) Hope that the Lookahead works as it's supposed to.
Seems simple enough? Here's a screenshot, where you can clearly see that the Lookhead doesn't appear underneath the user input of 111. Below is my code. Any help would be greatly appreciated.
library(shiny)
library(shinysky)
options(shiny.trace = F) # change to T for trace
DF <- data.frame(ID=c("111", "222", "333", "444"), info=c("info1", "info2", "info3", "info4"))
runApp(list(ui = pageWithSidebar(
headerPanel("This is a test"),
sidebarPanel(
helpText("I couldn't live without StackOverflow"),
sliderInput("range",
label = "Pick how many rows you want in your dataframe:",
min = 2, max = 4, value = 2, step=1),
helpText("After subsetting the dataframe using the controls above, can we make the Lookahead work?"),
textInput.typeahead(
id="thti"
,placeholder="type ID and info"
,local=subset(DF)
,valueKey = "ID"
,tokens=c(1,2)
,template = HTML("<p class='repo-language'>{{info}}</p> <p class='repo-name'>{{ID}}</p> <p class='repo-description'></p>"))
),
mainPanel(textOutput("text1"),
htmlOutput("text"),
tableOutput('table')
)
),
server = function(input, output, session) {
subsetDF <- reactive({ DF <- DF[1:input$range, ]
DF
})
output$text <- renderUI({
str <- paste("This is how many rows you've chosen for your dataframe:",
input$range)
HTML(paste(str, sep = '<br/>'))
})
output$table <- renderTable(subsetDF())
}
)
)